package com.auxgroup.smarthome.inner.service;

import com.auxgroup.bridge.business.inner.dto.MultiFunctionListDto;
import com.auxgroup.smarthome.BeanUtils;
import com.auxgroup.smarthome.inner.entity.MultiFunctionListEntity;
import com.auxgroup.smarthome.inner.repo.MultiFunctionListRepo;
import com.auxgroup.smarthome.syscode.BusinessCode;
import com.auxgroup.smarthome.web.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: laiqiuhua.
 * @Date: 2017/10/17 11:19.
 */
@Service
@Transactional
public class MultiFunctionListService {
    @Autowired
    private MultiFunctionListRepo multiFunctionListRepo;

    public ApiResponse saveMultiFunctionList(MultiFunctionListDto multiFunctionListDto) {
        long count = multiFunctionListRepo.count();
        if(count >= 1) {
            return ApiResponse.prompt(BusinessCode.MULTI_FUNCTION_LIST_ONLY_ONE);
        }
        MultiFunctionListEntity multiFunctionListEntity = BeanUtils.copyAttrs(new MultiFunctionListEntity(), multiFunctionListDto);
        multiFunctionListRepo.save(multiFunctionListEntity);
        return ApiResponse.ok(multiFunctionListEntity.getFunId());
    }

    public ApiResponse updateMultiFunctionList(String id, MultiFunctionListDto multiFunctionListDto) {
        MultiFunctionListEntity multiFunctionListEntity = multiFunctionListRepo.findOne(id);
        if(null == multiFunctionListEntity) {
            ApiResponse.prompt(BusinessCode.MULTI_FUNCTION_LIST_NOT_EXISTS);
        }
        BeanUtils.copyAttrs(multiFunctionListEntity, multiFunctionListDto);
        multiFunctionListRepo.save(multiFunctionListEntity);
        return ApiResponse.ok(multiFunctionListEntity.getFunId());
    }

    public ApiResponse deleteMultiFunctionList(String id) {
        MultiFunctionListEntity multiFunctionListEntity = multiFunctionListRepo.findOne(id);
        if(null == multiFunctionListEntity) {
            ApiResponse.prompt(BusinessCode.MULTI_FUNCTION_LIST_NOT_EXISTS);
        }
        multiFunctionListRepo.delete(id);
        return ApiResponse.ok();
    }

    public ApiResponse<List<MultiFunctionListEntity>> getMultiFunctionList() {
        List<MultiFunctionListEntity> multiFunctionListEntity = multiFunctionListRepo.findAll();
        return ApiResponse.ok(multiFunctionListEntity);
    }

}
